from zope.component import getMultiAdapter
from zope.interface import implements

from plone.locking.interfaces import ILockable

from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.permissions import ModifyPortalContent

from redomino.externaleditor.browser.interfaces import IExtEditAppInfo
from redomino.externaleditor.browser.interfaces import IExternalEditorAvailable

class ExternalEditorAvailable(BrowserView):
    """ Check for current object is externally editable """
    implements(IExternalEditorAvailable)

    def available(self):
        """ Return True or False if the current object is externally editable """
        plone_context_state = getMultiAdapter((self.context, self.request), name=u'plone_context_state')
        is_context_locked = plone_context_state.is_locked()

        if is_context_locked:
            return False
        else:
            plone_tools = getMultiAdapter((self.context, self.request), name=u'plone_tools')
            properties = plone_tools.properties().site_properties
    
            external_editor = properties.getProperty('external_editor')
            return external_editor



class ExtEditAppInfo(BrowserView):
    implements(IExtEditAppInfo)
    def checkModificationDate(self):
        """Returns the modification date of the context """
        modification_date = self.context.ModificationDate()
        # Returns modification date to client
        return modification_date
    def checkEditPermission(self):
        """Checks if the current user has edit permission on the context"""
        context = self.context
        portal_membership = getToolByName(context, 'portal_membership')

        can_edit = portal_membership.checkPermission(ModifyPortalContent, context)
        if can_edit:
            return '1'
        else:
            return '0'
        
    def doUnlock(self):
        """Unlocks the context"""
        context = self.context
    
        locking = ILockable(context, None)
        if locking:
            if not locking.can_safely_unlock():
                return '0'
            else:
                locking.unlock()
                return '1'
    def doLock(self):
        """Locks the context"""
        context = self.context
    
        locking = ILockable(context, None)
        #import pdb; pdb.set_trace()
        if locking:
            if not locking.can_safely_unlock():
                lockI = locking.lock_info()
                return '0####'+lockI[0]['creator']
            else: # we are locking the content
                locking.lock()
                lockI = locking.lock_info()
                return '1####'+'/'.join(context.getPhysicalPath()).lstrip('/')+'####'+lockI[0]['token']
        else:
            return '0'

    def checkLock(self):
        """ Returs 0 if the context is not locked or locked by current user
            Returns 1#### + the username otherwise"""
        context = self.context
        plone_lock_info = context.restrictedTraverse('@@plone_lock_info')

        locked = plone_lock_info.is_locked()
        locked_by_me = plone_lock_info.is_locked_for_current_user()
        lock_info = plone_lock_info.lock_info()

        if locked:
            if not locked_by_me:
                return '0'
            else:
                return '1####'+lock_info['creator']
        else:
            return '0'
